import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hanzi/flutter_hanzi.dart';

import 'mizi_grid_painter.dart';

class HanziCopybookPage extends StatefulWidget {
  const HanziCopybookPage({super.key});

  @override
  State<HanziCopybookPage> createState() => _HanziCopybookPageState();
}

class _HanziCopybookPageState extends State<HanziCopybookPage> {

  String? chineseData;
  int strokeIndex = 0;
  @override
  void initState() {
    rootBundle.loadString("assets/镈.json").then((value) {
      setState(() {
        chineseData = value;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("字帖"),
      ),
      body: buildHandziCopybook(),
    );
  }


  Widget buildHandziCopybook() {
    return Center(
        child: SizedBox(
          width: 400,
          height: 400,
          child: Stack(
            fit: StackFit.expand,
            children: [
              CustomPaint(
                painter: MiziGridPainter(),
              ),
              if (chineseData != null)
                HanziCopybookWidget(
                  chineseData: chineseData!,
                  backgroundColor: Colors.blueGrey[50]!,
                  onStrokeResult: (strokeIndex, result) {
                    print("笔划:$strokeIndex, ${result.name}");
                  },
                  onComplete: (){
                    print("全部完成");
                  },
                ),
            ],
          ),
        ),
      );
  }
}


